/**
 * 
 */
package org.xlab.baseframework.web.component;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.xlab.baseframework.domain.SystemParameter;
import org.xlab.baseframework.domain.view.SystemParameterVO;
import org.xlab.baseframework.exception.SystemParameterNotFoundException;
import org.xlab.baseframework.service.api.ISystemParameterService;
import org.xlab.baseframework.system.Declare;

import com.jensjansson.pagedtable.PagedTable;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * @author MaKuBEX
 * 
 */
public class ParameterFunctionLayout extends AbstractBaseHorizontalSplitPanel {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 5125717361511166766L;

	public PagedTable parameterTable;
	public VerticalLayout buttons;

	@Autowired(required = true)
	@Qualifier("bpf_systemParameterService")
	private ISystemParameterService systemParameterService;
	@Autowired(required = true)
	@Qualifier("bpf_parameterEditWindow")
	private ParameterEditWindow parameterEditWindow;
	private BeanItemContainer<SystemParameterVO> allSystemParameter;

	/**
	 * <p>
	 * 默认构造函数
	 * </p>
	 * 
	 * @throws SystemParameterNotFoundException
	 */
	public ParameterFunctionLayout() {
		super();
		super.setMargin(false);
		super.setWidth(Declare.HUNDRED_PERCENT);
	}

	/**
	 * @see com.vaadin.ui.AbstractComponentContainer#attach()
	 */
	@SuppressWarnings("serial")
	public void attach() {
		super.attach();
		this.parameterTable = new PagedTable();
		this.parameterTable.setSelectable(true);
		this.parameterTable.setImmediate(true);
		try {
			List<SystemParameter> parameterList = this.systemParameterService
					.findAll();
			List<SystemParameterVO> voList = this.systemParameterService
					.convertToVOList(parameterList);
			allSystemParameter = new BeanItemContainer<SystemParameterVO>(
					SystemParameterVO.class, voList);
			this.parameterTable.setContainerDataSource(allSystemParameter);
			this.parameterTable.setVisibleColumns(new String[] {
					SystemParameterVO.FIELD_CODE,
					SystemParameterVO.FIELD_VALUE,
					SystemParameterVO.FIELD_DESCRIPTION,
					SystemParameterVO.FIELD_CREATED_BY_NAME,
					SystemParameterVO.FIELD_S_CREATED_TIME,
					SystemParameterVO.FIELD_LASTUPDATED_BY_NAME,
					SystemParameterVO.FIELD_S_LASTUPDATED_TIME });
			this.parameterTable.setPageLength(25);
		} catch (SystemParameterNotFoundException e) {
			e.printStackTrace();
		}

		VerticalLayout left = new VerticalLayout();
		left.setSizeFull();
		left.addComponent(this.parameterTable);
		left.addComponent(this.parameterTable.createControls());
		this.parameterTable.setSizeFull();

		this.buttons = new VerticalLayout();
		this.buttons.setSpacing(true);
		this.buttons.setMargin(true);

		Button add = new Button("Add");
		final Button edit = new Button("Edit");
		edit.setEnabled(false);
		final Button delete = new Button("Delete");
		delete.setEnabled(false);

		this.buttons.addComponent(add);
		this.buttons.addComponent(edit);
		this.buttons.addComponent(delete);
		this.buttons.setWidth("120");

		super.addComponent(left);
		super.addComponent(this.buttons);
		super.setSplitPosition(140f, UNITS_PIXELS, true);

		this.parameterTable.addListener(new Table.ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				SystemParameterVO parameterVO = (SystemParameterVO) event
						.getProperty().getValue();
				if (parameterVO == null) {
					edit.setEnabled(false);
					delete.setEnabled(false);
				} else {
					delete.setEnabled(true);
					edit.setEnabled(true);
				}
			}
		});

		edit.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				Item editItem = ParameterFunctionLayout.this.parameterTable
						.getItem(ParameterFunctionLayout.this.parameterTable
								.getValue());
				parameterEditWindow.setEditItem(editItem);
				parameterEditWindow.setWidth(Declare.HUNDRED_PERCENT);
				ParameterFunctionLayout.super.getWindow().addWindow(
						parameterEditWindow);
			}
		});

		delete.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				SystemParameterVO parameterVO = (SystemParameterVO) ParameterFunctionLayout.this.parameterTable
						.getValue();
				SystemParameter parameterInstance = ParameterFunctionLayout.this.systemParameterService
						.parseFromVO(parameterVO);
				ParameterFunctionLayout.this.systemParameterService
						.deleteParameter(parameterInstance);

			}
		});

		this.parameterTable.addListener(new RepaintRequestListener() {

			public void repaintRequested(RepaintRequestEvent event) {
				List<SystemParameter> parameterList;
				try {
					parameterList = ParameterFunctionLayout.this.systemParameterService
							.findAll();
					List<SystemParameterVO> voList = ParameterFunctionLayout.this.systemParameterService
							.convertToVOList(parameterList);
					ParameterFunctionLayout.this.allSystemParameter = new BeanItemContainer<SystemParameterVO>(
							SystemParameterVO.class, voList);
					SystemParameterVO parameterVO = (SystemParameterVO) ParameterFunctionLayout.this.parameterTable
							.getValue();
					if (parameterVO == null) {
						delete.setEnabled(false);
						edit.setEnabled(false);
					} else {
						delete.setEnabled(true);
						edit.setEnabled(true);
					}
				} catch (SystemParameterNotFoundException e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * @param systemParameterService the systemParameterService to set
	 */
	public void setSystemParameterService(
			ISystemParameterService systemParameterService) {
		this.systemParameterService = systemParameterService;
	}

	/**
	 * @param parameterEditWindow the parameterEditWindow to set
	 */
	public void setParameterEditWindow(ParameterEditWindow parameterEditWindow) {
		this.parameterEditWindow = parameterEditWindow;
	}

}
